﻿<Window x:Class="DataBinding.ValidationTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataBinding"
        Title="Validation" Height="508" Width="409">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="2*"></RowDefinition>
        </Grid.RowDefinitions>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>

            <ListBox Name="lstProducts" Margin="5" DisplayMemberPath="ModelName">
            </ListBox>

            <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal" Margin="5,2,5,10">
                <Button Margin="2,0,0,0"  Padding="2"
              Click="cmdGetProducts_Click">Get Products</Button>             
            </StackPanel>
        </Grid>

        <GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" ResizeBehavior="PreviousAndNext"                        
              Height="5"></GridSplitter>
        <Border Grid.Row="2" Padding="7" Margin="7" Background="LightSteelBlue">
            <Grid Name="gridProductDetails" DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" 
                  Validation.Error="validationError">
                <Grid.Resources>

                    <Style TargetType="{x:Type TextBox}">
                        <Setter Property="Validation.ErrorTemplate">
                            <Setter.Value>
                                <ControlTemplate>
                                    <DockPanel LastChildFill="True">
                                        <TextBlock DockPanel.Dock="Right"
                      Foreground="Red" FontSize="14" FontWeight="Bold"
                        ToolTip="{Binding ElementName=adornerPlaceholder,
                        Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                               >*</TextBlock>
                                        <Border BorderBrush="Green" BorderThickness="1">
                                            <AdornedElementPlaceholder Name="adornerPlaceholder"></AdornedElementPlaceholder>
                                        </Border>
                                    </DockPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="Validation.HasError" Value="true">
                                <Setter Property="ToolTip"
                  Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                </Grid.RowDefinitions>

                <TextBlock Margin="7">Model Number:</TextBlock>
                <TextBox Margin="5" Grid.Column="1">
                    <TextBox.Text>
                        <Binding Path="ModelNumber" NotifyOnValidationError="true">
                            <Binding.ValidationRules>
                                <DataErrorValidationRule></DataErrorValidationRule>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

                <TextBlock Margin="7" Grid.Row="1">Model Name:</TextBlock>
                <TextBox Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding Path=ModelName}"></TextBox>

                <TextBlock Margin="7" Grid.Row="2">Unit Cost:</TextBlock>
                <TextBox Margin="5" Grid.Row="2" Grid.Column="1">
                    <TextBox.Text>
                        <Binding Path="UnitCost" NotifyOnValidationError="true" StringFormat="{}{0:C}">
                            <Binding.ValidationRules>
                                <local:PositivePriceRule Max="999.99"></local:PositivePriceRule>
                                <ExceptionValidationRule></ExceptionValidationRule>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBlock Margin="7,7,7,0" Grid.Row="3">Description:</TextBlock>
                <TextBox Margin="7" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" 
                 VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Text="{Binding Path=Description}"></TextBox>

                <StackPanel Orientation="Horizontal" Margin="3" Grid.Row="5" Grid.ColumnSpan="2" HorizontalAlignment="Right">                
                    <Button Click="cmdUpdateProduct_Click" IsDefault="True"
                  Margin="2" Padding="10,2,10,2">Update</Button>
                    <Button Click="cmdGetExceptions_Click" 
                  Margin="2" Padding="10,2,10,2">Get Validation Errors</Button>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</Window>
